﻿/*
 * prototbuf readme: 2. 安装python后, 需要在protobuf2.5 的python 目录下执行命令 Python setup.py install <br>**
 * 
 */


using UnityEditor;
using UnityEngine;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using LuaFramework;

public class BuildProtobuf : EditorWindow
{
    static List<string> paths = new List<string>();
    static List<string> files = new List<string>();

    static string genToolDir = "F:/autochest/protobuf/protoc-gen-lua-master";
    static string protoBufSourceFileDir = genToolDir + "/gen";

    static string GENTOOL_DIR_KEY = "genToolDir";
    static string PROTOBUF_SOURCEFILE_DIR_KEY = "protoBufSourceFileDir";

    static void Recursive(string path)
    {
        string[] names = Directory.GetFiles(path);
        string[] dirs = Directory.GetDirectories(path);
        foreach (string filename in names)
        {
            string ext = Path.GetExtension(filename);
            if (ext.Equals(".meta")) continue;
            files.Add(filename.Replace('\\', '/'));
        }
        foreach (string dir in dirs)
        {
            if (dir.IndexOf("protobuf") >= 0)
            {
                continue;
            }
            paths.Add(dir.Replace('\\', '/'));
            Recursive(dir);
        }
    }

    [MenuItem("Tools/BuildProtobuf")]
    static void Init()
    {
        BuildProtobuf sc = (BuildProtobuf)EditorWindow.GetWindow(typeof(BuildProtobuf));
        protoBufSourceFileDir = getStrByKey(PROTOBUF_SOURCEFILE_DIR_KEY, protoBufSourceFileDir);
        genToolDir = getStrByKey(GENTOOL_DIR_KEY, genToolDir);
    }

    public static string getStrByKey(string key, string value)
    {
        if(PlayerPrefs.HasKey(key))
        {
            return PlayerPrefs.GetString(key);
        }

        return protoBufSourceFileDir;
    }

    public static void setStrByKey(string key, string value)
    {
        PlayerPrefs.SetString(key, value);
    }


    private void OnGUI()
    {
        GUILayout.TextField(protoBufSourceFileDir);
        if (GUILayout.Button("Choose Protocol Source File Path"))
        {
            string newDir = EditorUtility.OpenFolderPanel("Choose Path", "", "");
            if(newDir!="" || newDir!= protoBufSourceFileDir)
            {
                protoBufSourceFileDir = newDir;
                setStrByKey(PROTOBUF_SOURCEFILE_DIR_KEY, protoBufSourceFileDir);
            }
        }

        GUILayout.TextField(genToolDir);
        if(GUILayout.Button("Choose Protocol Gen Tool Path"))
        {
            string newDir = EditorUtility.OpenFolderPanel("Choose Path", "", "");
            if (newDir != "" || newDir != genToolDir)
            {
                genToolDir = newDir;
                setStrByKey(GENTOOL_DIR_KEY, genToolDir);
            }
        }

        if(GUILayout.Button("BuildProtobufFile"))
        {
            BuildProtobufFile();
        }
    }

    public static void BuildProtobufFile() {

        paths.Clear(); files.Clear(); Recursive(protoBufSourceFileDir);
        string protoc = "protoc.exe";
        string output = "Lua/Protol";
        string outputdir = Path.Combine(AppConst.FrameworkRoot, output);
        string[] filesarr = Directory.GetFiles(outputdir);
        for (int i = 0; i < filesarr.Length; i++)
        {
            File.Delete(filesarr[i]);
        }

        string batDir = genToolDir + "/plugin/build.bat";

        foreach (string f in files) {
            string name = Path.GetFileName(f);
            string ext = Path.GetExtension(f);
            if (!ext.Equals(".proto")) continue;

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = genToolDir + "/gen/" + protoc;
      

            info.Arguments = " --plugin=protoc-gen-lua="+ batDir + " --lua_out="+ outputdir + " " + name;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.UseShellExecute = true;
            info.WorkingDirectory = protoBufSourceFileDir;
            info.ErrorDialog = true;
            Util.Log(info.FileName + " " + info.Arguments);

            Process pro = Process.Start(info);
            pro.WaitForExit();
        }

        AssetDatabase.Refresh();


        Util.Log("BuildProtobufFile Done!");
    }
}